package cn.xscrum.vtsp.service.impl;

import cn.xscrum.common.core.text.Convert;
import cn.xscrum.common.exception.BusinessException;
import cn.xscrum.common.utils.DateUtils;
import cn.xscrum.common.utils.StringUtils;
import cn.xscrum.framework.util.ShiroUtils;
import cn.xscrum.vtsp.domain.VtspAdviserManage;
import cn.xscrum.vtsp.mapper.VtspAdviserManageMapper;
import cn.xscrum.vtsp.service.IVtspAdviserManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 志愿者顾问管理信息Service业务层处理
 *
 * @author liuwl
 * @date 2021-10-01
 */
@Service
public class VtspAdviserManageServiceImpl implements IVtspAdviserManageService
{
    @Autowired
    private VtspAdviserManageMapper vtspAdviserManageMapper; //定义Mapper接口对象

    /**
     * 查询志愿者顾问管理ID
     *
     * @param adviserId 志愿者顾问信息ID
     * @return 志愿者顾问信息
     */
    @Override
    public VtspAdviserManage selectVtspAdviserManageById(Long adviserId)
    {
        return vtspAdviserManageMapper.selectVtspAdviserManageById(adviserId);
    }

    /**
     * 查询志愿者顾问管理列表
     *
     * @param vtspAdviserManage 志愿者顾问信息
     * @return 志愿者顾问信息
     */
    @Override
    public List<VtspAdviserManage> selectVtspAdviserManageList(VtspAdviserManage vtspAdviserManage)
    {
        return vtspAdviserManageMapper.selectVtspAdviserManageList(vtspAdviserManage);
    }

    /**
     * 新增志愿者顾问管理
     *
     * @param vtspAdviserManage 志愿者顾问信息
     * @return 新增结果
     */
    @Override
    public int insertVtspAdviserManage(VtspAdviserManage vtspAdviserManage)
    {
        //获取创建人名称
        vtspAdviserManage.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        //获取创建时间
        vtspAdviserManage.setCreateTime(DateUtils.getNowDate());
        return vtspAdviserManageMapper.insertVtspAdviserManage(vtspAdviserManage);
    }

    /**
     * 修改志愿者顾问管理
     *
     * @param vtspAdviserManage 志愿者顾问信息
     * @return 修改结果
     */
    @Override
    public int updateVtspAdviserManage(VtspAdviserManage vtspAdviserManage)
    {
        //获取更改人名称
        vtspAdviserManage.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        //获取更改时间
        vtspAdviserManage.setUpdateTime(DateUtils.getNowDate());
        return vtspAdviserManageMapper.updateVtspAdviserManage(vtspAdviserManage);
    }

    /**
     * 批量删除志愿者顾问管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 批量删除结果
     */
    @Override
    public int deleteVtspAdviserManageByIds(String ids)
    {
        return vtspAdviserManageMapper.deleteVtspAdviserManageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除志愿者顾问管理
     *
     * @param adviserId 志愿者顾问信息ID
     * @return 删除结果
     */
    @Override
    public int deleteVtspAdviserManageById(Long adviserId)
    {
        return vtspAdviserManageMapper.deleteVtspAdviserManageById(adviserId);
    }

    /**
     * 批量删除志愿者顾问参与的活动
     *
     * @param ids 需要删除的数据ID
     * @return 批量删除结果
     */
    public int deleteVtspActivityAdviserByIds(String ids)
    {
        return vtspAdviserManageMapper.deleteVtspActivityAdviserByIds(Convert.toStrArray(ids));
    }

    /**
     * 导入志愿者顾问管理文件
     *
     * @param adviserList 志愿者顾问信息列表
     * @return 结果
     */
    @Override
    public String importList(List<VtspAdviserManage> adviserList)
    {
        //判空
        if (StringUtils.isNull(adviserList) || adviserList.size() == 0)
        {
            throw new BusinessException("导入数据不能为空！");
        }
        int successNum = 0;     //导入成功数量
        int failureNum = 0;     //导入失败数量
        StringBuilder successMsg = new StringBuilder();     //导入成功响应
        StringBuilder failureMsg = new StringBuilder();     //导入失败响应

        //对导入的数据进行循环判断
        for (VtspAdviserManage vtspAdviserManage : adviserList)
        {
            //导入成功
            try
            {
                //插入
                this.insertVtspAdviserManage(vtspAdviserManage);
                successNum++;
                //导入成功提示
                successMsg.append("<br/>" + successNum + "志愿者顾问信息导入成功！");
            }
            //导入失败
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "志愿者顾问信息导入失败！";
                failureMsg.append(msg + e.getMessage());
            }
        }
        //结果反馈
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询志愿者顾问参与的活动
     *
     * @param adviserId
     * @return 志愿者顾问参与的活动信息
     */
    @Override
    public List<VtspAdviserManage> selectVtspActivityList(Long adviserId)
    {
        return vtspAdviserManageMapper.selectVtspActivityList(adviserId);
    }
}
